package com.code516.service.impl;

import com.code516.dao.WorkDao;
import com.code516.entity.WorkEntity;
import com.code516.entity.pojo.WorkStudentPojo;
import com.code516.service.WorkService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class WorkServiceImpl implements WorkService {

	@Resource
	private WorkDao workDao;
	
	public List<WorkStudentPojo> findAll() {
		List<WorkStudentPojo> workStudentPojos = null;
		try {
			workStudentPojos = workDao.findAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return workStudentPojos;
	}

	public WorkStudentPojo findById(String workId) {
		WorkStudentPojo workEntity = null;
		try {
			workEntity = workDao.findById(Integer.parseInt(workId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return workEntity;
	}

	
	public WorkEntity findByWorkUrl(String workUrl) {
		WorkEntity workEntity = null;
		try {
			workEntity = workDao.findByWorkUrl(workUrl);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return workEntity;
	}

	public int saveWorkEntity(WorkEntity workEntity, int stuId) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		workEntity.setReleaseTime(sdf.format(new Date()));
		workEntity.setStuId(stuId);
		workEntity.setIsUsed(1);
		try {
			workDao.saveWorkEntity(workEntity);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		return 1;
	}
	
	/**
	 * ����ѧ��Id��ȡ��Ӧѧ��������Ʒ
	 */
	public List<WorkStudentPojo> findByStuId(int stuId){
		List<WorkStudentPojo> workStudentPojos = null;
		try {
			workStudentPojos = workDao.findByStuId(stuId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return workStudentPojos;
	}
	
	
	public int findTotal(int stuId){
		int total = 0;
		try {
			total = workDao.findTotal(stuId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return total;
	}

}
